<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Add Property
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 29.09.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddProperty
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 29.09.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxAddProperty extends ajaxAction{

        public function process(){
            $nodeid = (int)$_REQUEST['node'];
            $pageid = (int)$_REQUEST['page'];
            $name   = $_REQUEST['pname'];
            $value  = $_REQUEST['pvalue'];
            
            //now check if there is already a property with that name at that level
            if($pageid > 0){
                $propid = $this->db->getOne("SELECT id FROM properties AS p, property_page_mapping AS pm WHERE pm.propid = p.id AND pm.pageid = $pageid AND p.name = '$name'");
            }else{
                $propid = $this->db->getOne("SELECT id FROM properties AS p, property_node_mapping AS pm WHERE pm.propid = p.id AND pm.nodeid = $nodeid AND p.name = '$name'");
            }
            
            //there is no property named like this at that level
            if(!$propid){
                //transaction start
                $this->db->StartTrans();
                
                //add property
                $this->db->Execute("INSERT INTO properties(name,value) VALUES('$name','$value')");
                $newPropID = $this->db->getOne("SELECT last_value FROM properties_id_seq");
                
                //add mapping
                if($pageid > 0){
                    $this->db->Execute("INSERT INTO property_page_mapping(propid,pageid) VALUES($newPropID,$pageid)");
                }else{
                    $this->db->Execute("INSERT INTO property_node_mapping(propid,nodeid) VALUES($newPropID,$nodeid)");
                }
                
                //transaction end
                $this->db->CompleteTrans();
                
                $this->r->id     = $newPropID;
                $this->r->status = true;
            }else{
                $this->r->status = false;
            }
        }
    }
?>